using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using SchemaProvider;
using Kenly.DBFramework;

namespace OracleSchemaProvider
{
    public class OracleSchemaLoader : SchemaLoaderBase
    {
        
        //private IDatabase _database = DatabaseFactory.CreateOracleDatabase();

        //public override string ConnectionString
        //{
        //    get
        //    {
        //        return base.ConnectionString;
        //    }
        //    set
        //    {
        //        base.ConnectionString = value;
        //        _database.ConnectionString = base.ConnectionString;
        //    }
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //protected override DataTable GetForeignKeyTable()
        //{
        //    return _database.CreateViewCommandExecutor().ExecuteDataTable(OracleCommandUtility.ListForeignKey(), false);
        //}

        ///// <summary>
        ///// There is no identity information.
        ///// </summary>
        ///// <returns></returns>
        //protected override DataTable GetIdentityTable()
        //{
        //    DataTable dt=new DataTable();
        //    dt.Columns.Add("IdentityName");
        //    dt.Columns.Add("TableName");
        //    return dt;
        //}

        //protected override DataTable GetPrimaryKeyTable()
        //{
        //    return _database.CreateViewCommandExecutor().ExecuteDataTable(OracleCommandUtility.ListPrimaryKeys(), false);
        //}

        //protected override DataTable GetTableNames()
        //{
        //    return _database.CreateViewCommandExecutor().ExecuteDataTable(OracleCommandUtility.ListTableNames(), false);
        //}

        //protected override DataTable GetViewNames()
        //{
        //    return _database.CreateViewCommandExecutor().ExecuteDataTable(OracleCommandUtility.ListViewNames(), false);
        //}

        //private DataSet LoadTableSchemas(bool isTable)
        //{
        //    DataSet schemaDataSet = new DataSet();
        //    List<string> tableNames ;
        //    tableNames = isTable?base.ListTableNames():base.ListViewNames();
        //    try
        //    {
        //        _database.Open();
        //        foreach (string tableName in tableNames)
        //        {
        //            DataTable tempTable = _database.ExecuteDataTable("select * from "+tableName+" where 1 = 2",false);
        //            tempTable.TableName = tableName;
        //            if (tempTable != null)
        //            {
        //                schemaDataSet.Tables.Add(tempTable);
        //            }
        //        }
        //        _database.Close();
        //    }
        //    catch
        //    {
        //        _database.Close();
        //    }

        //    return schemaDataSet;
        //}

        //protected override DataTable GetProcedureParameterTable()
        //{
        //    return _database.CreateViewCommandExecutor().ExecuteDataTable(OracleCommandUtility.ListProcedures(), false);
        //}

        //public override DataSet LoadTableSchemas()
        //{
        //    return LoadTableSchemas(true);
        //}

        //public override DataSet LoadViewSchemas()
        //{
        //    return LoadTableSchemas(false);
        //}


        private IDatabase _database = null;


        protected override IDatabase Database
        {
            get
            {
                if (_database == null)
                {
                    _database = DatabaseFactory.CreateOracleDatabase();
                }
                return _database;
            }
            set
            {
                _database = value;
            }
        }

        public override void ShowAboutBox()
        {
            OracleAboutBox oracleAboutBox = new OracleAboutBox();
            oracleAboutBox.ShowDialog();
        }

        public override Dictionary<string, string> ListIdentities()
        {
            Dictionary<string, string> identityDictionary = new Dictionary<string, string>();
            return identityDictionary;
        }

        protected override string ListIdentitiesSql()
        {
            return "";
        }

        protected override string ListPrimaryKeysSql()
        {
            return OracleCommandUtility.ListPrimaryKeys();
        }

        protected override string ListForeignKeySql()
        {
            return OracleCommandUtility.ListForeignKey();
        }

        protected override string ListProcedureParametersSql()
        {
            return OracleCommandUtility.ListProcedures();
        }

        protected override string ListTableNamesSql()
        {
            return OracleCommandUtility.ListTableNames();
        }

        protected override string ListViewNamesSql()
        {
            return OracleCommandUtility.ListViewNames();
        }

        public override List<ProcedureParameter> ListProcedureParameters()
        {
            List<ProcedureParameter> procedureParameters = new List<ProcedureParameter>();
            DataTable procedureParameterTable = this.Database.CreateViewCommandExecutor().ExecuteDataTable(
                this.ListProcedureParametersSql(), false);

            foreach (DataRow row in procedureParameterTable.Rows)
            {
                ProcedureParameter parameter = new ProcedureParameter();
                parameter.ProcedureName = row["ProcedureName"].ToString();
                parameter.ParameterName = row["ParameterName"].ToString();
                parameter.ParameterType = row["ParameterType"].ToString();
                parameter.Length = row["Length"].ToString();
                parameter.IsOutput = row["InOut"].ToString()=="OUT"?"1":"0";
                procedureParameters.Add(parameter);
            }
            return procedureParameters;
        }
    }
}
